package com.carboy.launch.adapter.setting;

import java.util.List;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.carboy.launch.R;

/**
 * wifi参数Adapter
 */
public class WifiAdapter extends BaseAdapter {

	private Context context; // 上下文
	private List<ScanResult> listScanResults; // 数据源

	public WifiAdapter(Context context, List<ScanResult> listScanResults) {
		super();
		this.context = context;
		this.listScanResults = listScanResults;
	}

	public void setListScanResults(List<ScanResult> listScanResults) {
		this.listScanResults = listScanResults;
	}

	public int getCount() {
		if (listScanResults == null) {
			return 0;
		}
		return listScanResults.size();
	}

	public ScanResult getItem(int position) {
		return listScanResults.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_wifi_setting, null);
			holder = new ViewHolder();
			convertView.setTag(holder);
			holder.iws_wname_tv = (TextView) convertView
					.findViewById(R.id.iws_wname_tv);
			holder.iws_details_igview = (ImageView) convertView
					.findViewById(R.id.iws_details_igview);
			holder.iws_rlevel_igview = (ImageView) convertView
					.findViewById(R.id.iws_rlevel_igview);
			holder.iws_lock_igview = (ImageView) convertView
					.findViewById(R.id.iws_lock_igview);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// getBSSID() 获取BSSID
		// getDetailedStateOf() 获取客户端的连通性
		// getHiddenSSID() 获得SSID 是否被隐藏
		// getIpAddress() 获取IP 地址
		// getLinkSpeed() 获得连接的速度
		// getMacAddress() 获得Mac 地址
		// getRssi() 获得802.11n 网络的信号
		// getSSID() 获得SSID
		// getSupplicanState() 返回具体客户端状态的信息
		// ========================================================
		// level 转换rssi 说明http://www.zhihu.com/question/21106590
		// RSSI = level - NoiceFloor
		// NoiceFloor一般取-96dBm
		// 这样如果 level 是 -60dBm, RSSI 就是 36
		ScanResult scanResult = listScanResults.get(position);

		holder.iws_wname_tv.setText(scanResult.SSID); // wifi名
		int rLevel = WifiManager.calculateSignalLevel(scanResult.level, 5); // 信号强度。
		this.setLevelImage(holder.iws_rlevel_igview, rLevel); // 设置对应的信号强度图片

		// LogOutput.d(scanResult.SSID + "-----------------" +
		// scanResult.capabilities);
		// scanResult.capabilities 加密方式

		if (isCapabilities(scanResult.capabilities) == 0) {
			holder.iws_lock_igview.setBackgroundResource(0);
		} else {
			holder.iws_lock_igview
					.setBackgroundResource(R.drawable.icon_lock_close);
		}
		return convertView;
	}

	class ViewHolder {
		TextView iws_wname_tv;
		ImageView iws_details_igview;
		ImageView iws_rlevel_igview;
		ImageView iws_lock_igview;
	}

	/** 设置对应的信号强度图片 */
	private void setLevelImage(ImageView imageView, int level) {
		switch (level) {
		case 0:
			imageView.setBackgroundResource(R.drawable.icon_rssi_level_0);
			break;
		case 1:
			imageView.setBackgroundResource(R.drawable.icon_rssi_level_0);
			break;
		case 2:
			imageView.setBackgroundResource(R.drawable.icon_rssi_level_1);
			break;
		case 3:
			imageView.setBackgroundResource(R.drawable.icon_rssi_level_2);
			break;
		case 4:
			imageView.setBackgroundResource(R.drawable.icon_rssi_level_3);
			break;
		}
	}

	/**
	 * 检查对应的加密方式
	 * 
	 * @param capabilities
	 *            传入加密类型
	 * @return
	 */
	private int isCapabilities(String capabilities) {
		int isWap = capabilities.indexOf("WPA"); // 判断是否wpa方式
		if (isWap != -1) {
			return 2;
		}

		int isWep = capabilities.indexOf("WEP"); // 判断是否Wep方式
		if (isWep != -1) {
			return 1;
		}

		int isEss = capabilities.indexOf("ESS"); // 判断是否开放
		if (isEss != -1) {
			return 0;
		}
		return 0;
	}
}
